{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1 align=\"center\">SimpleITK: image analysis for all levels of programming expertise</h1>\n",
    "\n",
    "## Newcomers to Jupyter notebooks:\n",
    "1. We use two types of cells, code and markdown.\n",
    "2. To run a code cell, select it (mouse or arrow key so that it is highlighted) and then press shift+enter which also moves focus to the next cell or ctrl+enter which doesn't.\n",
    "3. Closing the browser window does not close the Jupyter server. To close the server, go to the terminal where you ran it and press ctrl+c twice.\n",
    "\n",
    "For additional details see the [Jupyter project documentation](https://jupyter.org/documentation) on Jupyter Notebook or JupyterLab.\n",
    "\n",
    "## Convenience\n",
    "By default the contents of the Jupyter notebooks do not occupy the full browser window width. To take advantage of the full window width you can either configure each notebook independently by adding the following into a code cell:\n",
    "```\n",
    "from IPython.core.display import display, HTML\n",
    "display(HTML(\"<style>.container { width:100% !important; }</style>\"))\n",
    "```\n",
    "Or apply this configuration to all notebooks by adding the following to\n",
    "the custom.css jupyter configuration file:\n",
    "```\n",
    ".container { width:100% !important; }\n",
    "```\n",
    "On OSX/Linux this file is found in `~/.jupyter/custom/custom.css` on windows it is\n",
    "found in `C:\\Users\\[your_user_name]\\.jupyter\\custom\\custom.css`.\n",
    "\n",
    "## Environment Setup for Course\n",
    "\n",
    "This notebook should be run prior to arriving at the course venue, as it <b>requires network connectivity</b>."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First, lets check that you have the SimpleITK version which you expect."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import SimpleITK as sitk\n",
    "from downloaddata import fetch_data, fetch_data_all\n",
    "\n",
    "from ipywidgets import interact\n",
    "\n",
    "print(sitk.Version())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, we check that the auxiliary program(s) are correctly installed in your environment.\n",
    "\n",
    "We expect that you have an external image viewer installed. The default viewer is <a href=\"https://fiji.sc/#download\">Fiji</a>. If you have another viewer (i.e. ITK-SNAP or 3D Slicer) you will need to set an environment variable to point to it. This is done using an environment variable which can also be set from within a notebook as shown below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "simpleitk_error_allowed": "Exception thrown in SimpleITK ImageViewer_Execute:"
   },
   "outputs": [],
   "source": [
    "# Retrieve an image from the network, read it and display using the external viewer\n",
    "image_viewer = sitk.ImageViewer()\n",
    "# Uncomment the line below to change the default external viewer to your viewer of choice and test that it works.\n",
    "#image_viewer.SetApplication('/Applications/ITK-SNAP.app/Contents/MacOS/ITK-SNAP')\n",
    "\n",
    "image_viewer.Execute(sitk.ReadImage(fetch_data(\"SimpleITK.jpg\")))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we check that the ipywidgets will display correctly. When you run the following cell you should see a slider.\n",
    "\n",
    "If you don't see a slider please shutdown the Jupyter server, at the Anaconda command line prompt press Control-c twice, and then run the following command:\n",
    "\n",
    "```jupyter nbextension enable --py --sys-prefix widgetsnbextension```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "interact(lambda x: x, x=(0,10));"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, we download all of the data used in the notebooks in advance. This step is necessary as we will be running the notebooks without network connectivity.\n",
    "\n",
    "This may take a couple of minutes depending on your network."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "fetch_data_all('data', os.path.join('data','manifest.json'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"01_spatial_transformations.ipynb\"><h2 align=right>Next &raquo;</h2></a>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
